package com.globalwave.common;

import java.util.ArrayList;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class ArrayPageList<T> extends ArrayList<T> {

	public ArrayPageList() {
	    super() ;	
	}
	
	public ArrayPageList(int size) {
		super(size) ;
	}
	
    private static final long serialVersionUID = 1L;
    
    private long total ;
    private int pageIndex ;

    public static final int PAGEINDEX_NO_TOTAL_WITH_PAGE = -1 ;
    public static final int PAGEINDEX_NO_PAGE = -2 ;
    

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }
    
    public String asXML() {
        return this.asXML(null) ;
    }
    
    public String asXML(Object obj) {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("root") ;
        
        root.addElement("total").addText(this.total + "") ;
        root.addElement("pageIndex").addText(this.pageIndex + "") ;
        
        try {
            if (obj != null) {
                if (obj instanceof Element) {
                    root.add((Element)obj) ;
                } else {
                    root.add(Convertor.object2Xml(obj)) ;
                }
            }
            
            root.add(Convertor.list2Xml(this,"datas")) ;
        } catch (Exception e) {
            root.addElement("message").addText("Conver array page list to xml error!") ;
        }
        
        return root.asXML() ;
    }

}
